
TOOL.Category   = "Wire - Display"
TOOL.Name       = "Bitmap Editor"
TOOL.Command    = nil
TOOL.ConfigName = ""
TOOL.Tab			= "Wire"

local editor

drawontoolgun = CreateClientConVar("bitmap_ontoolgun", 0, true, false )

// Add Default Language translation (saves adding it to the txt files)
if ( CLIENT ) then

	language.Add( "Tool_wire_bitmap_name", "Bitmap Editor" )
	language.Add( "Tool_wire_bitmap_desc", "Edit Bitmaps and send it to server!" )
	language.Add( "Tool_wire_bitmap_0", "Use context menu." )
	
end

function TOOL:LeftClick( trace )

	return false

end

function TOOL:RightClick( trace )

	return false
end



local function BitmapEditor()
	
	local panel = vgui.Create( "DFrame" ) 
	
	local button = vgui.Create( "DButton", panel)
	local buttonManager = vgui.Create( "DButton", panel)
	local buttonMemory = vgui.Create( "DButton", panel)
	local buttonSave = vgui.Create( "DButton", panel)
	
	local colorM = vgui.Create( "DColorMixer", panel)
	local RedWang = vgui.Create( "DNumberWang", panel)
	local GreenWang = vgui.Create( "DNumberWang", panel)
	local BlueWang = vgui.Create( "DNumberWang", panel)
	local AlphaWang = vgui.Create( "DNumberWang", panel)
	
	local buttonPencil = vgui.Create( "DButton", panel)
	local buttonPaintcan = vgui.Create( "DButton", panel)
	local buttonSelect = vgui.Create( "DButton", panel)
	local buttonGetcolor = vgui.Create( "DButton", panel)
	
	local buttonLine = vgui.Create( "DButton", panel)
	local buttonBox = vgui.Create( "DButton", panel)
	local buttonCircle = vgui.Create( "DButton", panel)
	
	local checkFill = vgui.Create( "DCheckBox", panel)
	local labelFill = vgui.Create( "DLabel", panel)
	
	editor = vgui.Create( "BMPedit", panel)
	
	local buttonX = vgui.Create( "DButton", panel)
	
	
		panel:SetSize( 800,600 )
		panel:Center( )
		panel:SetTitle( "Bitmap Editor" )
		panel:MakePopup()
		panel.EditorMode=0
		
		buttonX:SetSize( 20,20 )
		buttonX:SetPos( 780,0 )
		buttonX:SetText( "X" )
		buttonX.DoClick = function()
			LocalPlayer().BMPTemp=editor.bmp
			panel:Remove()
		end
		
		colorM:SetPos( 15, 300 )
		colorM:SetSize( 175, 125 )
		
		RedWang:SetPos( 170, 300 )
		RedWang:SetMax(255)
		RedWang:SetDecimals( 0 )
		RedWang:SetSize( 45 , 20 )
		RedWang:SetValue(255)
	
		GreenWang:SetPos( 170, 335 )
		GreenWang:SetMax(255)
		GreenWang:SetDecimals( 0 )
		GreenWang:SetSize( 45 , 20 )
		GreenWang:SetValue(255)
	
		BlueWang:SetPos( 170, 370 )
		BlueWang:SetMax(255)
		BlueWang:SetDecimals( 0 )
		BlueWang:SetSize( 45 , 20 )
		BlueWang:SetValue(255)
	
		AlphaWang:SetPos( 170, 405 )
		AlphaWang:SetMax(255)
		AlphaWang:SetDecimals( 0 )
		AlphaWang:SetSize( 45 , 20 )
		AlphaWang:SetValue(255)
	
	function RedWang:OnValueChanged(wang,val)
		colorM:SetColor(Color(RedWang:GetValue(),GreenWang:GetValue(),BlueWang:GetValue(),AlphaWang:GetValue()))
	end
	
	function GreenWang:OnValueChanged(wang,val)
		colorM:SetColor(Color(RedWang:GetValue(),GreenWang:GetValue(),BlueWang:GetValue(),AlphaWang:GetValue()))
	end
	
	function BlueWang:OnValueChanged(wang,val)
		colorM:SetColor(Color(RedWang:GetValue(),GreenWang:GetValue(),BlueWang:GetValue(),AlphaWang:GetValue()))
	end
	
	function AlphaWang:OnValueChanged(wang,val)
		colorM:SetColor(Color(RedWang:GetValue(),GreenWang:GetValue(),BlueWang:GetValue(),AlphaWang:GetValue()))
	end
	
	local old = colorM.ColorCube.OnMouseReleased
	colorM.ColorCube.OnMouseReleased = function( ... )
		local clr = colorM:GetColor()
		r, g, b, a = clr.r, clr.g, clr.b, 255-colorM.AlphaBar:GetSlideY()*255
		RedWang:SetValue( r )
		GreenWang:SetValue( g )
		BlueWang:SetValue( b )
		AlphaWang:SetValue( a )
		old( ... )
	end

	local old = colorM.RGBBar.OnMouseReleased
	colorM.RGBBar.OnMouseReleased = function(...)
		colorM.ColorCube:OnMouseReleased()
		old(...)
	end
	local old = colorM.AlphaBar.OnMouseReleased
	colorM.AlphaBar.OnMouseReleased = function(...)
		colorM.ColorCube:OnMouseReleased()
		old(...)
	end
	
	
		editor:SetSize( 550, 550 )
		editor:SetPos( 220, 50 )
		function editor:GetColor()
			local col = colorM:GetColor()
			col.a = 255 - (colorM.AlphaBar:GetSlideY() * 255)
			if col.a == 0 then return Color(0,0,0,0) end
			return col
		end
		function editor:GetThick()
			return NumSlider:GetValue()
		end
		function editor:SetEditorColor(col)
			RedWang:SetValue( col.r )
			GreenWang:SetValue( col.g )
			BlueWang:SetValue( col.b )
			AlphaWang:SetValue( col.a )
			colorM:SetColor( col )
		end
	
	
		buttonPencil:SetText( " " )
		buttonPencil:SetPos( 15, 430 )
		buttonPencil:SetSize( 40,40 )
		local old = buttonPencil.Paint
		buttonPencil.Paint = function(...)
			old(...)
			if panel.EditorMode == 0 then
				draw.RoundedBox(8, 8, 8, 24, 24, Color(0,255,0,100) )
			end
			surface.SetDrawColor(255,255,255,255)
			surface.SetTexture(surface.GetTextureID("BMPpencil"))
			surface.DrawTexturedRect(12,12,16,16)
		end
		buttonPencil.DoClick = function()
			panel.EditorMode=0
			editor.mode=0
		end
		
	
		buttonPaintcan:SetText( " " )
		buttonPaintcan:SetPos( 68, 430 )
		buttonPaintcan:SetSize( 40,40 )
		local old = buttonPaintcan.Paint
		buttonPaintcan.Paint = function(...)
			old(...)
			if panel.EditorMode == 1 then
				draw.RoundedBox(8, 8, 8, 24, 24, Color(0,255,0,100) )
			end
			surface.SetDrawColor(255,255,255,255)
			surface.SetTexture(surface.GetTextureID("BMPpaintcan"))
			
			surface.DrawTexturedRect(12,12,16,16)
		end
		buttonPaintcan.DoClick = function()
			panel.EditorMode=1
			editor.mode=1
		end
	
	
		buttonSelect:SetText( " " )
		buttonSelect:SetPos( 122, 430 )
		buttonSelect:SetSize( 40,40 )
		local old = buttonSelect.Paint
		buttonSelect.Paint = function(...)
			old(...)
			if panel.EditorMode == 2 then
				draw.RoundedBox(8, 8, 8, 24, 24, Color(0,255,0,100) )
			end
			surface.SetDrawColor(255,255,255,255)
			surface.SetTexture(surface.GetTextureID("BMPSelect"))
			surface.DrawTexturedRect(12,12,16,16)
		end
		buttonSelect.DoClick = function()
			panel.EditorMode=2
			editor.mode=2
		end
		
	
		buttonGetcolor:SetText( " " )
		buttonGetcolor:SetPos( 175, 430 )
		buttonGetcolor:SetSize( 40,40 )
		local old = buttonGetcolor.Paint
		buttonGetcolor.Paint = function(...)
			old(...)
			if panel.EditorMode == 3 then
				draw.RoundedBox(8, 8, 8, 24, 24, Color(0,255,0,100) )
			end
			surface.SetDrawColor(255,255,255,255)
			surface.SetTexture(surface.GetTextureID("BMPgetcolor"))
			surface.DrawTexturedRect(12,12,16,16)
		end
		buttonGetcolor.DoClick = function()
			panel.EditorMode=3
			editor.mode=3
		end
	
	
		buttonLine:SetText( " " )
		buttonLine:SetPos( 15, 483 )
		buttonLine:SetSize( 40,40 )
		local old = buttonLine.Paint
		buttonLine.Paint = function(...)
			old(...)
			if panel.EditorMode == 4 then
				draw.RoundedBox(8, 8, 8, 24, 24, Color(0,255,0,100) )
			end
			surface.SetDrawColor(255,255,255,255)
			surface.SetTexture(surface.GetTextureID("BMPline"))
			surface.DrawTexturedRect(12,12,16,16)
		end
		buttonLine.DoClick = function()
			panel.EditorMode=4
			editor.mode=4
		end
		
		
		buttonBox:SetText( " " )
		buttonBox:SetPos( 68, 483 )
		buttonBox:SetSize( 40,40 )
		local old = buttonBox.Paint
		buttonBox.Paint = function(...)
			old(...)
			if panel.EditorMode == 5 then
				draw.RoundedBox(8, 8, 8, 24, 24, Color(0,255,0,100) )
			end
			surface.SetDrawColor(255,255,255,255)
			surface.SetTexture(surface.GetTextureID("BMPbox"))
			surface.DrawTexturedRect(12,12,16,16)
		end
		buttonBox.DoClick = function()
			panel.EditorMode=5
			editor.mode=5
		end
		
		
		buttonCircle:SetText( " " )
		buttonCircle:SetPos( 122, 483 )
		buttonCircle:SetSize( 40,40 )
		local old = buttonCircle.Paint
		buttonCircle.Paint = function(...)
			old(...)
			if panel.EditorMode == 6 then
				draw.RoundedBox(8, 8, 8, 24, 24, Color(0,255,0,100) )
			end
			surface.SetDrawColor(255,255,255,255)
			surface.SetTexture(surface.GetTextureID("BMPcircle"))
			surface.DrawTexturedRect(12,12,16,16)
		end
		buttonCircle.DoClick = function()
			panel.EditorMode=6
			editor.mode=6
		end
		
		checkFill:SetPos( 187, 500 )
		checkFill:SetValue(true)
		checkFill.OnChange = function(self)
			editor.FillShape = self:GetChecked()
		end
		
		labelFill:SetPos( 170, 480 )
		labelFill:SetText("Fill shape?")
		
		buttonMemory:SetText( "Save to Memory" )
		buttonMemory:SetPos( 5,80 )
		buttonMemory:SetSize( 190,40 )
		buttonMemory.DoClick = function()
			local Spanel = vgui.Create( "DFrame" )
				Spanel:SetSize( 200,120 )
				Spanel:Center()
				Spanel:SetTitle( "Save to Memory" )
				Spanel:MakePopup()
			
			local SLabel = vgui.Create("DLabel", Spanel)
				SLabel:SetPos(10,20)
				SLabel:SetText( "Enter file name:" )
				SLabel:SetWide(180)
			
			local SText = vgui.Create("DTextEntry", Spanel)
				SText:SetPos(10,40)
				SText:SetWide(180)
			
			local SButton = vgui.Create( "DButton", Spanel)
				SButton:SetPos(10,70)
				SButton:SetSize(180,40)
				SButton:SetText("SAVE")
				SButton.DoClick = function()
					if SText:GetValue() == "" then return end
					if not LocalPlayer().bmp then LocalPlayer().bmp = {} end 
					if LocalPlayer().bmp[SText:GetValue()] then 
						local SSpanel = vgui.Create( "DFrame" )
							SSpanel:Center()
							SSpanel:SetTitle( "Do you want to replace?" )
							SSpanel:MakePopup()
						
						local SSLabel = vgui.Create("DLabel", SSpanel)
							SSLabel:SetPos(10,30)
							SSLabel:SetText( SText:GetValue().." exists. Do you want to replace it?" )
							SSLabel:SizeToContents()
							SSpanel:SetSize( SSLabel:GetWide()+20, 100 )
						
						local YesButton = vgui.Create( "DButton", SSpanel)
							YesButton:SetPos(10,50)
							YesButton:SetSize(80,40)
							YesButton:SetText("Yes")
							YesButton.DoClick = function()
								LocalPlayer().bmp[SText:GetValue()] = table.Copy(editor.bmp)
								LocalPlayer().bmp[SText:GetValue()].owner = LocalPlayer()
								
								SSpanel:Close()
								Spanel:Close()
							end
							
						local NoButton = vgui.Create( "DButton", SSpanel)
							NoButton:SetPos(SSLabel:GetWide()-70,50)
							NoButton:SetSize(80,40)
							NoButton:SetText("No")
							NoButton.DoClick = function()
								SSpanel:Close()
							end
							
					else
						
						LocalPlayer().bmp[SText:GetValue()] = table.Copy(editor.bmp)
						LocalPlayer().bmp[SText:GetValue()].owner = LocalPlayer()
						
						Spanel:Close()
					end
				end
		end
		
		buttonSave:SetText( "Save on HDD" )
		buttonSave:SetPos( 5,130 )
		buttonSave:SetSize( 190,40 )
		buttonSave.DoClick = BMPManager
	
		buttonManager:SetText( "Open Bitmap Manager" )
		buttonManager:SetPos( 5,180 )
		buttonManager:SetSize( 190,40 )
		buttonManager.DoClick = BMPManager
		
		button:SetText( "New Bitmap" )
		button:SetPos( 5,30 )
		button:SetSize( 190,40 )
		-- ******************* NEW BITMAP *********************
		button.DoClick = function()
			
			local NBpanel = vgui.Create( "DFrame" ) 
				NBpanel:SetSize( 200,180 )
				NBpanel:Center()
				NBpanel:SetTitle( "New Bitmap" )
				NBpanel:MakePopup()
			
			local NumSliderX = vgui.Create( "DNumSlider", NBpanel )
				NumSliderX:SetPos( 15,30 )
				NumSliderX:SetWide( 175 )
				NumSliderX:SetText( "Size X" )
				NumSliderX:SetMin( 1 )
				NumSliderX:SetMax( 256 )
				NumSliderX:SetDecimals( 0 )
				NumSliderX:SetValue(32)
			
			local NumSliderY = vgui.Create( "DNumSlider", NBpanel )
				NumSliderY:SetPos( 15,75 )
				NumSliderY:SetWide( 175 )
				NumSliderY:SetText( "Size Y" )
				NumSliderY:SetMin( 1 )
				NumSliderY:SetMax( 256 )
				NumSliderY:SetDecimals( 0 )
				NumSliderY:SetValue(32)
			
			local CheckBoxAlpha = vgui.Create( "DCheckBoxLabel", NBpanel )
				CheckBoxAlpha:SetPos( 20,120 )
				CheckBoxAlpha:SetText( "Set alpha to 0?" )
				CheckBoxAlpha:SizeToContents()
			
			local NBbutton = vgui.Create( "DButton", NBpanel)
				NBbutton:SetText( "Create!" )
				NBbutton:SetPos( 5,150 )
				NBbutton:SetSize( 190,25 )
				NBbutton.DoClick = function( button )
					editor:Clear(math.Clamp(NumSliderX:GetValue(),1,256),math.Clamp(NumSliderY:GetValue(),1,256),CheckBoxAlpha:GetChecked())
					NBpanel:Close()
				end
			-- ******************* END OF NEW BITMAP *********************
		end
		
end

function TOOL.BuildCPanel( panel )
	
	panel:SetSpacing( 10 )
	panel:SetName( "Bitmap Editor" )
	panel:SetHeight(100)
	
	
	local button = vgui.Create( "DButton", panel)
		button:SetText( "Open bitmap editor" )
		button.DoClick = BitmapEditor

	panel:AddItem(button)
	
	local buttonManager = vgui.Create( "DButton", panel)
		buttonManager:SetText( "Open BMP-Manager" )
		buttonManager.DoClick = BMPManager
		
	panel:AddItem(buttonManager)
		
	local checkDrawTool = vgui.Create( "DCheckBoxLabel", panel)
		checkDrawTool:SetText( "Draw Bitmap on toolgun monitor? (decreases fps!!!)" )
		checkDrawTool:SetConVar("bitmap_ontoolgun")
		checkDrawTool:SizeToContents()
	
	panel:AddItem(checkDrawTool)
end



-- ******************* CUSTOM TOOL RENDER ********************
function TOOL:RenderToolScreen()
	cam.Start2D()
	
	surface.SetDrawColor(255, 255, 255, 255)
	surface.SetTexture(surface.GetTextureID("BMPEditor_bg"))
    surface.DrawTexturedRectUV(0,0,256,256,7,7)
	
	ply = LocalPlayer()
	
	if editor and editor.bmp and editor.bmp.Xsize and editor.bmp.Ysize and drawontoolgun:GetBool() then
		local sizeX = editor.bmp.Xsize
		local sizeY = editor.bmp.Ysize
		local scaleX = sizeX/64
		local scaleY = sizeY/64
		
		for x=0, 63 do
			for y=0, 63 do
				surface.SetDrawColor(editor.bmp[math.floor(x*scaleX)+1][math.floor(y*scaleY)+1])
				surface.DrawRect( x*4, y*4, 4, 4)
			end
		end
	elseif ply.BMPTemp and ply.BMPTemp.Xsize and ply.BMPTemp.Ysize and drawontoolgun:GetBool() then
		local sizeX = ply.BMPTemp.Xsize
		local sizeY = ply.BMPTemp.Ysize
		local scaleX = sizeX/64
		local scaleY = sizeY/64
		
		for x=0, 63 do
			for y=0, 63 do
				surface.SetDrawColor(ply.BMPTemp[math.floor(x*scaleX)+1][math.floor(y*scaleY)+1])
				surface.DrawRect( x*4, y*4, 4, 4)
			end
		end
	end
	
	cam.End2D()
end